# 导入模块
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select


# 定义创建浏览器对象的方法
def create_browser():
    """
    创建谷歌浏览器对象-->打开浏览器
    :return: 返回创建的浏览器对象.
    """
    my_browser = webdriver.Chrome()
    return my_browser

"""创建浏览器配置项"""


# 浏览器加载项配置
def create_loading_browser(name):
    driver_object = webdriver.ChromeOptions()
    """user_data_dir自己浏览器的路径
    """
    user_data_dir = rf"--user-data-dir=C:\Users\{name}\AppData\Local\Google\Chrome\User Data"
    driver_object.add_argument(user_data_dir)
    my_driver = webdriver.Chrome(options=driver_object)
    return my_driver
    
class Base(object):
    # 定义属性
    def __init__(self, driver):
        """
        给每个实例化的对象赋予对象属性
        :param driver: 把浏览器对象通过 参数driver传给每一个对象
        """
        self.driver = driver

    # 定义方法

    def base_open_web(self, url):
        """
        打开网页
        :param url: 被访问的网址
        :return:
        """
        try:
            self.driver.get(url)
            self.driver.maximize_window()
        except:
            return "浏览器打开失败,无法输入网址"

    def base_input_massage(self, element_located, message_input, wait_time=3):
        """
        进行输入内容操作
        :param element_located: 被定位的元素
        :param message_input: 要输入的内容
        :param wait_time: 最多等待时间
        :return:
        """
        try:
            WebDriverWait(self.driver, wait_time).until(EC.presence_of_element_located(element_located)).send_keys(
                message_input)
        except:
            return "元素没定位到,无法输入内容"

    def base_click(self, element_located, wait_time=3):
        """
        进行点击操作
        :param element_located: 被定位的元素
        :param wait_time: 最多等待时间
        :return:
        """
        try:
            WebDriverWait(self.driver, wait_time).until(EC.presence_of_element_located(element_located)).click()
        except:
            return "元素没定位到,无法进行点击操作"

    def base_get_text(self, element_located, wait_time=3):
        """

        :param element_located: 被定位的元素
        :param wait_time: 最多等待时间
        :return: 返回元素的文本内容
        """
        try:
            result = WebDriverWait(self.driver, wait_time).until(EC.presence_of_element_located(element_located)).text
        except Exception as e:
            return "元素没定位到,无法获取元素文本值", e
        else:
            return result

    def base_drop_down_menu(self, select_element, option_number, wait_time=3):
        """
        勾选下拉框的选项
        :param select_element: select标签元素
        :param wait_time:显示等待时间
        :param option_number:option选项的索引号
        :return:
        """
        try:
            select = Select(WebDriverWait(self.driver, wait_time).until(EC.presence_of_element_located(select_element)))
            select.select_by_index(option_number)
        except:
            return "元素没定位到,无法根据索引号勾选下拉选项"

    def base_checkbox(self, checkbox_element, wait_time=3):
        """
        勾选多选框的选项(全选)
        :param checkbox_element: 多选框元素
        :param wait_time:显示等待时间
        :return:
        """
        try:
            check_box_element = WebDriverWait(self.driver, wait_time).until(
                EC.presence_of_all_elements_located(checkbox_element))
            for i in check_box_element:
                if i.is_selected():  # 判断是否勾选
                    pass
                else:  # 没有勾选就点击勾选
                    i.click()
        except:
            return "元素没定位到,无法勾选多选框选项"

    def base_name_iframe_window(self, frame_element):
        """
        进入iframe的窗口 通过name/id和索引号
        :param frame_element:name/id属性值  索引号
        :return:
        """
        try:
            self.driver.switch_to.frame(frame_element)
        except:
            return "iframe元素没定位到,无法进入iframe窗口"

    def base_into_iframe_window(self, element_located, wait_time=3):
        """
        进入iframe的窗口,通过定位iframe元素
        :param element_located:定位的iframe元素
        :param wait_time:显示等待时间
        :return:
        """
        try:
            frame_element = WebDriverWait(self.driver, wait_time).until(EC.presence_of_element_located(element_located))
            self.driver.switch_to.frame(frame_element)
        except:
            return "iframe元素没定位到,无法进入iframe窗口"

    def base_quit_all_iframe(self):
        """
        退出iframe的所有窗口
        :return:
        """
        self.driver.switch_to.default_content()

    def base_quit_current_iframe(self):
        """
        退出iframe的当前窗口
        :return:
        """
        self.driver.switch_to.parent_frame()

    def base_many_select_option(self, element_located, index: int, num: int, wait_time=3):
        """
        使用多元素定位多个select和以及下面的option选项
        :param element_located: select多元素定位
        :param index:  列表中select元素的索引号
        :param num: option的索引号
        :param wait_time:显示等待时间
        :return:
        """
        try:
            select_elements = WebDriverWait(self.driver, wait_time).until(
                EC.presence_of_all_elements_located(element_located))  # 生成一个列表
            select = Select(select_elements[index])
            select.select_by_index(num)
        except Exception as e:
            return "元素没有定位到,无法选择optin选项", e

    def base_many_elements(self, elements_located, num: int, wait_time=3):
        """
        通过显示等待定位多元素,选择要操作的单元素
        :param elements_located: 多元素定位器
        :param num:索引号
        :param wait_time: 显示等待时间
        :return:
        """
        try:
            many_elements = WebDriverWait(self.driver, wait_time).until(
                EC.presence_of_all_elements_located(elements_located))  # 生成一个列表
            one_element = many_elements[num]
        except Exception as e:
            return "多元素未定位到,无法从多元素列表中,选择定位的元素进行输入操作", e
        else:
            return one_element

    def base_many_elements_send(self, elements_located, num: int, message_input, wait_time=3):
        """
        通过显示等待定位多元素,选择要操作的单元素 输入内容
        :param elements_located: 多元素定位器
        :param num:索引号
        :param message_input:输入的内容
        :param wait_time: 显示等待时间
        :return:
        """
        try:
            many_elements = WebDriverWait(self.driver, wait_time).until(
                EC.presence_of_all_elements_located(elements_located))  # 生成一个列表
            many_elements[num].send_keys(message_input)
        except Exception as e:
            return "多元素未定位到,无法从多元素列表中,选择定位的元素进行输入操作", e

    def base_many_elements_click(self, elements_located, num: int, wait_time=5):
        """
        通过显示等待定位多元素,选择要操作的单元素 点击
        :param elements_located: 多元素定位器
        :param num:索引号
        :param wait_time: 显示等待时间
        :return:
        """
        try:
            many_elements = WebDriverWait(self.driver, wait_time).until(
                EC.presence_of_all_elements_located(elements_located))  # 生成一个列表
            many_elements[num].click()
        except Exception as e:
            return "多元素未定位到,无法从多元素列表中,选择定位的元素进行输入操作", e

    def base_element_clear(self, element_clear):
        """
        进行实际标签的点击操作
        :param element_clear: 实际标签
        :param wait_time: 最多等待时间
        :return:
        """
        try:
            element_clear.clear()
        except:
            return "元素没找到,无法进行点击操作"

    def base_element_send_keys(self, element_send, message_input):
        """
        进行实际标签的输入操作
        :param element_send: 实际标签
        :param message_input: 输入内容
        :return:
        """
        try:
            element_send.send_keys(message_input)
        except:
            return "元素没没找到,无法输入内容"

    def base_element_click(self, element_click):
        """
        进行实际标签的点击操作
        :param element_click: 实际标签
        :return:
        """
        try:
            element_click.click()
        except:
            return "元素没找到,无法进行点击操作"

    def base_alert_accept(self, wait_time=5):
        """
        捕获弹窗,进行点击确认按钮
        :param wait_time: 最多等待时间
        :return:
        """
        try:
            WebDriverWait(self.driver, wait_time).until(EC.alert_is_present()).accept()
        except:
            print("没有捕获到弹窗,无法点击确认按钮")
            return False

    def base_alert_dismiss(self, wait_time=5):
        """
        捕获弹窗,进行点击删除按钮
        :param wait_time: 最多等待时间
        :return:
        """
        try:
            WebDriverWait(self.driver, wait_time).until(EC.alert_is_present()).dismiss()
        except:
            print("没有捕获到弹窗,无法点击取消按钮")
            return False
    def base_scroll_tail(self, num):
        """
        向下滑动滚动条
        :param num:向下拉的像素
        :return:
        """

        js_down = f"window.scrollTo(0,{num})"
        self.driver.execute_script(js_down)		
         
    def base_many_get_value(self, elements_located, num, wait_time=3):
        """
        多元素获取文本你值
        :param element_located: 被定位的元素
        :param wait_time: 最多等待时间
        :param num: 索引号
        :return: 返回元素的文本内容
        """
        try:
            many_elements = WebDriverWait(self.driver, wait_time).until(
                EC.presence_of_all_elements_located(elements_located))  # 生成一个列表
            result = many_elements[num].get_attribute("value")

        except Exception as e:
            return "元素没定位到,无法获取元素文本值", e
        else:
            return result


    def base_quit_web(self):

        """
        进行关闭浏览器+浏览器驱动的操作
        :return:
        """
        self.driver.quit()


if __name__ == '__main__':
    pass
